package com.fsm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/*******************
 *@Describe
 *@author 封神梦
 *@Time 2023/6/13
 *@Company http://com.yiview.com
 ****************/
public class SetRedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    public void set(String key, String value) {
        //1、通过redisTemplate设置值
        redisTemplate.boundSetOps("setKey").add("setValue1", "setValue2", "setValue3");

        //2、通过BoundValueOperations设置值
        BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
        setKey.add("setValue1", "setValue2", "setValue3");

        //3、通过ValueOperations设置值
        SetOperations setOps = redisTemplate.opsForSet();
        setOps.add("setKey", "SetValue1", "setValue2", "setValue3");
    }

    //2. 设置过期时间(单独设置)
    public void setExpire() {

        redisTemplate.boundValueOps("setKey").expire(1, TimeUnit.MINUTES);
        redisTemplate.expire("setKey", 1, TimeUnit.MINUTES);
    }

    //3.根据key获取Set中的所有值
    //1、通过redisTemplate获取值
    public void getValue() {
        Set set1 = redisTemplate.boundSetOps("setKey").members();

        //2、通过BoundValueOperations获取值
        BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
        Set set2 = setKey.members();

        //3、通过ValueOperations获取值
        SetOperations setOps = redisTemplate.opsForSet();
        Set set3 = setOps.members("setKey");
    }

    //4.根据value从一个set中查询,是否存在
    public void checkExits() {
        Boolean isEmpty = redisTemplate.boundSetOps("setKey").isMember("setValue2");
    }

    //5.获取Set缓存的长度
    public void getSize() {
        Long size = redisTemplate.boundSetOps("setKey").size();
    }

    //6.移除指定的元素
    public void removeValue() {
        Long result1 = redisTemplate.boundSetOps("setKey").remove("setValue1");
    }

    //7.移除指定的key
    public void deleteKey() {
        Boolean result2 = redisTemplate.delete("setKey");

    }
}
